﻿

#include "zyDateTimePick.h"


DarkMoonUI::zyDateTimePick::zyDateTimePick()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyDateTimePick_WndProc);
	m_obj->setChildWndProc(zyDateTimePick_ChildWndProc);
	m_obj->setDestroy(zyDateTimePick_Destroy);
}
DarkMoonUI::zyDateTimePick::~zyDateTimePick()
{

}
void DarkMoonUI::zyDateTimePick::OnNCDestroy()
{
	LONG_PTR hFontMonth = GetMember(L"hFontMonth");
	if (hFontMonth != 0) {
		//pEdit->getObject()->release();
		//delete pEdit;
		DeleteObject((HFONT)hFontMonth);
		SetMember(L"hFontMonth", 0);
	}
}
void WINAPI DarkMoonUI::zyDateTimePick::zyDateTimePick_Destroy(zyObject* obj)
{
	zyDateTimePick* zyb = (zyDateTimePick*)obj->getPtrData(L"this");
	zyb->OnNCDestroy();
	zyCtrol::zyCtrol_Destroy(obj);
}
LRESULT WINAPI DarkMoonUI::zyDateTimePick::zyDateTimePick_ChildWndProc(zyObject* obj, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	if (message == WM_NOTIFY) {
		NMHDR* nm = reinterpret_cast<NMHDR*>(lParam);
		if (nm->code == DTN_DATETIMECHANGE) {
			TRegEvent* evtPtr = (TRegEvent*)obj->getRegEvent(L"OnDateTimeSelChange");
			if (evtPtr != nullptr) {
				NMDATETIMECHANGE* dtChange = reinterpret_cast<NMDATETIMECHANGE*>(lParam);
				DOUBLE 局部_日期时间 = 0;
				SystemTimeToVariantTime(&dtChange->st, &局部_日期时间);
				evtPtr->handler<int>(nm->hwndFrom, 局部_日期时间, dtChange->dwFlags);
				*bHandle = true;
				return 0;
			}
		}
		else if (nm->code == DTN_USERSTRINGW) {
			TRegEvent* evtPtr = (TRegEvent*)obj->getRegEvent(L"OnUserString");
			if (evtPtr != nullptr) {
				NMDATETIMESTRING* lpDTstring = reinterpret_cast<NMDATETIMESTRING*>(lParam);
				DOUBLE dt = 0;
				evtPtr->handler<int>(nm->hwndFrom, lpDTstring->pszUserString, dt, lpDTstring->dwFlags);
				VariantTimeToSystemTime(dt, &lpDTstring->st);
				*bHandle = true;
				return 0;
			}
		}
	}
	return zyCtrol::zyCtrol_ChildWndProc(obj, hWnd, message, wParam, lParam, bHandle);
}
LRESULT WINAPI DarkMoonUI::zyDateTimePick::zyDateTimePick_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{
	if (uMsg == WM_NCDESTROY) {
		zyDateTimePick* zyb = (zyDateTimePick*)obj->getPtrData(L"this");
		zyb->OnNCDestroy();
	}
	return zyCtrol::zyCtrol_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandled);
}

BOOL DarkMoonUI::zyDateTimePick::创建(HWND 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_窗口风格 == 0)
		参数_窗口风格 = DTS_LONGDATEFORMAT;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口, 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格, 参数_扩展风格, L"SysDateTimePick32");
}

BOOL DarkMoonUI::zyDateTimePick::创建(zyWindow& 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_窗口风格 == 0)
		参数_窗口风格 = DTS_LONGDATEFORMAT;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口.getHwnd(), 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格, 参数_扩展风格, L"SysDateTimePick32");
}
bool DarkMoonUI::zyDateTimePick::是否空日期() {//当有 日期框_显示复选框 风格时，判断复选框是否未选中。
	SYSTEMTIME systime = { 0 };
	int ret = (int)sendMsg(DTM_GETSYSTEMTIME, 0, (LPARAM)&systime);
	return ret == GDT_NONE;
}
DOUBLE DarkMoonUI::zyDateTimePick::今天(DOUBLE 参数_今天) {
	DOUBLE 局部_日期时间;
	SYSTEMTIME systime = { 0 };
	if (参数_今天 == 0) {
		sendMsg(DTM_GETSYSTEMTIME, 0, (LPARAM)&systime);
		SystemTimeToVariantTime(&systime, &局部_日期时间);
		return 局部_日期时间;
	}
	else {
		VariantTimeToSystemTime(参数_今天, &systime);
		sendMsg(DTM_SETSYSTEMTIME, 0, (LPARAM)&systime);
		return 参数_今天;
	}
}
DOUBLE DarkMoonUI::zyDateTimePick::最小时间(DOUBLE 参数_最小时间) {
	SYSTEMTIME systimes = { 0 };
	DOUBLE 局部_日期时间;
	if (参数_最小时间 == 0) {
		sendMsg(DTM_GETRANGE, 0, (LPARAM)&systimes);
		SystemTimeToVariantTime(&systimes, &局部_日期时间);
		return 局部_日期时间;
	}
	else {
		VariantTimeToSystemTime(参数_最小时间, &systimes);
		sendMsg(DTM_SETRANGE, GDTR_MIN, (LPARAM)&systimes);
		return 参数_最小时间;
	}
}
DOUBLE DarkMoonUI::zyDateTimePick::最大时间(DOUBLE 参数_最大时间) {
	SYSTEMTIME systimes;
	DOUBLE 局部_日期时间;
	ZeroMemory(&systimes, sizeof(systimes)); // 初始化结构体，确保没有垃圾数据
	if (参数_最大时间 == 0) {
		sendMsg(DTM_GETRANGE, 0, (LPARAM)&systimes);
		SystemTimeToVariantTime(&systimes, &局部_日期时间);
		return 局部_日期时间;
	}
	else {
		VariantTimeToSystemTime(参数_最大时间, &systimes);
		sendMsg(DTM_SETRANGE, GDTR_MAX, (LPARAM)&systimes);
		return 参数_最大时间;
	}
}
int DarkMoonUI::zyDateTimePick::取月历颜色(int 参数_标志) {
	return (int)sendMsg(DTM_GETMCCOLOR, 参数_标志, 0);
}
bool DarkMoonUI::zyDateTimePick::置月历颜色(int 参数_颜色值, int 参数_标志) {
	return sendMsg(DTM_SETMCCOLOR, 参数_标志, 参数_颜色值) != -1;
}
bool DarkMoonUI::zyDateTimePick::置日期格式(const std::wstring& 参数_格式) {
	return sendMsg(DTM_SETFORMATW, 0, (LPARAM)参数_格式.c_str()) != 0;
}
bool DarkMoonUI::zyDateTimePick::取月历(zyMonthCal& 参数_月历) {//比如在月历控件显示状态才能获取。成功返回真，失败返回假。
	HWND 局部_句柄 = (HWND)sendMsg(DTM_GETMONTHCAL, 0, 0);
	if (局部_句柄 == 0 || IsWindow(局部_句柄) == 0)
		return false;
	return 参数_月历._BindWindow(局部_句柄);
}
Gdiplus::Font* DarkMoonUI::zyDateTimePick::取月历字体() {
	Gdiplus::Font* font = (Gdiplus::Font*)sendMsg(DTM_GETMCFONT, 0, 0);
	return font;
}
void DarkMoonUI::zyDateTimePick::置月历字体(Gdiplus::Font& 参数_字体) {
	Gdiplus::Font* hFont = (Gdiplus::Font*)GetMember(L"hFontMonth");
	if (hFont != nullptr)
		delete hFont;
	//hFont = 参数_字体.取字体句柄 ();
	SetMember(L"hFontMonth", (LONG_PTR)&参数_字体);
	sendMsg(DTM_SETMCFONT, (WPARAM)&参数_字体, 1);
}